<template>
  <el-card shadow="always" :body-style="{ padding: '20px' }">
    <el-form :inline="true" class="demo-form-inline" :disabled="disabled">
      <el-form-item label="一级分类">
        <el-select
          placeholder="请选择"
          v-model="(category1Id as number)"
          @change="category1Change"
        >
          <el-option
            v-for="category in categoryList1"
            :label="category.name"
            :value="category.id"
            :key="category.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="二级分类">
        <el-select
          placeholder="请选择"
          v-model="(category2Id as number)"
          @change="category2Change"
        >
          <el-option
            v-for="category in categoryList2"
            :label="category.name"
            :value="category.id"
            :key="category.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="三级分类">
        <el-select placeholder="请选择" v-model="(category3Id as number)">
          <el-option
            v-for="category in categoryList3"
            :label="category.name"
            :value="category.id"
            :key="category.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  name: "Category"
});
</script>

<script setup lang="ts">
import { onMounted } from "vue";
import { storeToRefs } from "pinia";
//引入attr的仓库
import useAttrStore from "@/stores/attr";

defineProps<{
  disabled: boolean;
}>();

//获取attr仓库实例对象
const attrStore = useAttrStore();

//接受attr仓库中的state数据
const {
  category1Id,
  category2Id,
  category3Id,
  categoryList1,
  categoryList2,
  categoryList3
} = storeToRefs(attrStore);

//初始化的时候请求一级列表
onMounted( () => {
    attrStore.getCategoryList1()
});

/**@category1Change 一级列表切换的事件 */
const category1Change = async () => {
  //每当一级列表切换的时候，清空二级三级列表
  category2Id.value = null;
  category3Id.value = null;
  categoryList2.value = [];
  categoryList3.value = [];
  await attrStore.getCategoryList2();
};

/**@category2Change 二级列表切换的事件 */
const category2Change = async () => {
  //每当二级列表切换的时候，清空三级列表
  category3Id.value = null;
  categoryList3.value = [];
  await attrStore.getCategoryList3();
};
</script>
